<template>
  <!-- 添加或修改出入库记录对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="产品编码" prop="code">
                <el-input v-model="form.code" placeholder="请输入产品编码" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出库数量" prop="daohuoCount">
                <el-input-number v-model="form.daohuoCount" placeholder="请输入出入库数量"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出库日期" prop="rukudate">
                <el-date-picker clearable
                                v-model="form.rukudate"
                                type="datetime"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择出入库日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="总价" prop="goodsTotalPrice">
                <el-input-number v-model="form.goodsTotalPrice" placeholder="请输入总价"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="成本单价" prop="goodsBuyPrice">
                <el-input-number v-model="form.goodsBuyPrice" placeholder="请输入成本单价"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="成本总金额" prop="goodsBuyTotal">
                <el-input v-model="form.goodsBuyTotal" placeholder="请输入成本总金额"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="操作工" prop="douser">
                <el-input type="textarea" v-model="form.douser" placeholder="请输入操作工"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="自制入库明细" prop="doremark">
                <el-input type="textarea" v-model="form.doremark" placeholder="请输入自制入库明细"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>

          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';
import {parseTime} from "@/utils/common";

export default {
  name: "LvChukumingxiForm",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rukudate: [
          {required: true, message: "入库日期不能为空", trigger: "blur"},
        ],
        code: [
          {required: true, message: "产品编码不等为空", trigger: "blur"},
        ],
        daohuoCount: [
          {required: true, message: "到货数量不能为空", trigger: "blur"}
        ],
      },
      url: {
        getById: '/lvRukumingxi/lvRukumingxi/',
        add: '/lvRukumingxi/lvRukumingxi/addOut',
        edit: '/lvRukumingxi/lvRukumingxi/edit',
      },
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        goodsTotalPrice: null,
        goodsBuyPrice: null,
        goodsBuyTotal: null,
        goodsProfit: null,
        saleDate: null,
        supplierId: null,
        supplierName: null,
        customerId: null,
        customerName: null,
        saleId: null,
        saleName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        projectId: null,
        projectName: null,
        status: "0",
        isfahuo: null,
        fahuoDate: null,
        code: null,
        dun: null,
        nos: null,
        isdo: null,
        mainId: null,
        qinggouId: null,
        caigouno: null,
        unit: null,
        daohuoId: null,
        zhijianDate: null,
        zhijianName: null,
        isZhijian: null,
        zhijiandan: null,
        daohuono: null,
        daohuodate: null,
        rukudate: null,
        rukuren: null,
        rukuno: null,
        daohuoCount: null,
        type: "1",
        isDo: null,
        douser: null,
        doremark: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAddByGoods(goods) {
      this.reset();
      this.open = true;
      this.form.rukudate = parseTime(new Date(), '{y}-{m}-{d}');
      this.form.goodsId  = goods.id;
      this.form.code = goods.code;
      this.title = "添加出库记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出入库记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },
  }
};
</script>
